<template>
  <div>
    <el-dialog
      :title="title"
      size="small"
      :close-on-click-modal="false"
      v-dialogDrag
      :visible.sync="visible"
    >
      <el-form
        size="small"
        :model="inputForm"
        ref="inputForm"
        v-loading="loading"
        :class="method === 'view' ? 'readonly' : ''"
        :disabled="method === 'view'"
        label-width="120px"
      >
        <el-row :gutter="15">
          <el-col
            :span="24"
            style="margin-bottom: 18px; color: rgb(42, 139, 216)"
            ><i class="el-icon-menu">客户信息：</i></el-col
          >
          <el-col :span="12">
            <el-form-item
              label="名称"
              prop="customerName.companyName"
              :rules="[
                {
                  required: true,
                  message: '客户名称不能为空',
                  trigger: 'blur',
                },
              ]"
            >
              <el-input
                v-model="inputForm.customerName.companyName"
                placeholder="请填写客户名称"
                :disabled="method !== 'add' &&(this.customerType==0 || this.customerType==2)"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="地址"
              prop="customerArea"
              :rules="[
                {
                  required: true,
                  message: '客户地址不能为空',
                  trigger: 'blur',
                },
              ]"
            >
              <el-input
                v-model="inputForm.customerArea"
                placeholder="请填写客户地址"
                :disabled="method !== 'add' &&(this.customerType==0 || this.customerType==2)"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col
            :span="24"
            style="margin-bottom: 18px; color: rgb(42, 139, 216)"
            ><i class="el-icon-menu">客户负责人信息：</i></el-col
          >
          <el-col :span="12">
            <el-form-item
              label="姓名"
              prop="personName.userName"
              :rules="[
                {
                  required: true,
                  message: '负责人姓名不能为空',
                  trigger: 'blur',
                },
              ]"
            >
              
              <el-input
                v-model="inputForm.personName.userName"
                placeholder="请填写负责人姓名"
                :disabled="method !== 'add' &&(this.customerType==0 || this.customerType==2)"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="电话"
              prop="personPhone"
              :rules="[
                {
                  required: true,
                  message: '负责人电话不能为空',
                  trigger: 'blur',
                  pattern: /(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/,
                },
              ]"
            >
              <el-input
                v-model="inputForm.personPhone"
                placeholder="请填写负责人电话"
                :disabled="method !== 'add' &&(this.customerType==0 || this.customerType==2)"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="personArea" :rules="[]">
              <el-input
                v-model="inputForm.personArea"
                placeholder="请填写负责人地址"
                :disabled="method !== 'add' &&(this.customerType==0 || this.customerType==2)"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="personCard"
             :rules="[
                {
                  required: true,
                  message: '身份证号不能为空',
                  trigger: 'blur',
                  pattern:
              /^[1-9][0-9]{5}(18|19|20)[0-9]{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)[0-9]{3}([0-9]|(X|x))/,
                },
              ]">
              <el-input
                v-model="inputForm.personCard"
                placeholder="请填写负责人身份证号"
               
                :disabled="method !== 'add' &&(this.customerType==0 || this.customerType==2)"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="部门"
              prop="personOffice"
              :rules="[
                {
                  required: true,
                  message: '负责人部门不能为空',
                  trigger: 'blur',
                },
              ]"
            >
              <el-input
                v-model="inputForm.personOffice"
                placeholder="请填写负责人部门"
                :disabled="method !== 'add' &&(this.customerType==0 || this.customerType==2)"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col
            :span="24"
            style="margin-bottom: 18px; color: rgb(42, 139, 216)"
            ><i class="el-icon-menu">客情负责人信息：</i></el-col
          >
          <el-col :span="12">
            <el-form-item
              label="姓名"
              prop="systemName.id"
              :rules="[
                {
                  required: true,
                  message: '系统负责人姓名不能为空',
                  trigger: 'blur',
                },
              ]"
            >
              <user-select
                :limit="1"
                :value="inputForm.systemName.id"
                @getValue='getValue(arguments)'
                :disabled="method !== 'add' && this.customerType==0"
              ></user-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="电话"
              prop="systemPhone"
              :rules="[
                {
                  required: true,
                  message: '系统负责人电话不能为空',
                  trigger: 'blur',
                },
              ]"
            >
              <el-input
                v-model="inputForm.systemPhone"
                placeholder="请填写系统负责人电话"
                :disabled="true"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col
            :span="24"
            style="margin-bottom: 18px; color: rgb(42, 139, 216)"
            ><i class="el-icon-menu">收付款及开票信息：</i></el-col
          >
          <el-col :span="12">
            <el-form-item
              label="收款方"
              prop="receivingSide"
              :rules="[
                { required: true, message: '收款方不能为空', trigger: 'blur' },
              ]"
            >
              <el-input
                v-model="inputForm.receivingSide"
                placeholder="请填写收款方"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="开户行"
              prop="bankname"
              :rules="[
                { required: true, message: '开户行不能为空', trigger: ['blur','change'] },
              ]"
            >
              <!-- <el-select v-model="inputForm.bankname" placeholder="请选择"  style="width: 100%;">
                  <el-option
                      v-for="(item,index) in $dictUtils.getDictList('zhaohang_chinese_bank')"
                      :key="index"
                      :label="item.label"
                      :value="item.value">
                  </el-option>
              </el-select> -->
              <el-input
                v-model="inputForm.bankname"
                placeholder="请填写开户行"
              ></el-input>
            </el-form-item>
          </el-col>
           <el-col :span="12">
            <el-form-item
              label="账号"
              prop="bankno"
              :rules="[
                { required: true, message: '账号不能为空', trigger: 'blur' },
              ]"
            >
              <el-input
                v-model="inputForm.bankno"
                placeholder="请填写账号"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纳税人识别号" prop="taxno"
                :rules="[{ required: true, message: '纳税人识别号不能为空', trigger: 'blur' },]">
              <el-input v-model="inputForm.taxno" placeholder="请填写纳税人识别号"     ></el-input>
           </el-form-item>
        </el-col>
        
        
        <el-col :span="12">
            <el-form-item label="开票信息" prop="billinginfo"
                :rules="[{ required: true, message: '开票信息不能为空', trigger: 'blur' },]">
              <el-input v-model="inputForm.billinginfo" placeholder="请填写开票信息"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="开票地址" prop="billingarea"
                :rules="[{ required: true, message: '开票地址不能为空', trigger: 'blur' },]">
              <el-input v-model="inputForm.billingarea" placeholder="请填写开票地址"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col
            :span="24"
            style="margin-bottom: 18px; color: rgb(42, 139, 216)"
            ><i class="el-icon-menu">资质信息：</i></el-col
          >
        <el-col :span="12">
            <el-form-item label="到期时间" prop="endtime"
                :rules="[{ required: true, message: '到期时间不能为空', trigger: 'blur' },]">
                <el-date-picker
                      v-model="inputForm.endtime"
                      type="datetime"
                      style="width: 100%;"
                      value-format="yyyy-MM-dd"
                      format="yyyy-MM-dd"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="客户资质" prop="qualifications"
                :rules="[{ required: true, message: '客户资质不能为空', trigger: 'blur' },]">
              <el-upload ref="qualifications"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl.hr/guestsentiment/guestSentiment`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                        $alert(`<img style='width:100%' src=' ${(file.response && file.response.url) || file.url}'/>`,  {
                          dangerouslyUseHTMLString: true,
                          showConfirmButton: false,
                          closeOnClickModal: true,
                          customClass: 'showPic'
                        });
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.qualifications = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.qualifications = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="qualificationsArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col>
          <el-col
            :span="24"
            style="margin-bottom: 18px; color: rgb(42, 139, 216)"
            ><i class="el-icon-menu">业务：</i></el-col
          >
          
          <el-col :span="12">
            <el-form-item
              label="业务"
              prop="business"
              :rules="[
                { required: true, message: '业务不能为空', trigger: 'blur' },
              ]"
            >
              <el-input
                type="textarea"
                v-model="inputForm.business"
                placeholder="请填写业务"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks" :rules="[]">
              <el-input
                type="textarea"
                v-model="inputForm.remarks"
                placeholder="请填写备注信息"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label-width="0">
              <el-tabs v-model="guestSentimentTab">
                <el-tab-pane label="客情礼品赠送">
                  <el-button
                    size="small"
                    @click="addGuestGiftRow"
                    type="primary"
                    >新增</el-button
                  >
                  <el-table
                    class="table"
                    size="small"
                    :cell-style="cellStyle"
                    :header-cell-class-name="cellHeadStyle"
                    :row-class-name="tableRowClassName"
                    :data="
                      inputForm.guestGiftList.filter(function (item) {
                        return item.delFlag !== '1';
                      })
                    "
                    style="width: 100%; border: 1px solid #d6cfe2"
                  >
                    <el-table-column
                      prop="time"
                      header-align="center"
                      align="center"
                      show-overflow-tooltip
                      label="日期"
                    >
                    </el-table-column>
                    <el-table-column
                      prop="giftname"
                      header-align="center"
                      align="center"
                      show-overflow-tooltip
                      label="名称"
                    >
                    </el-table-column>
                    <el-table-column
                      prop="giftcount"
                      header-align="center"
                      align="center"
                      show-overflow-tooltip
                      label="数量"
                    >
                    </el-table-column>
                    <el-table-column
                      prop="expressname"
                      header-align="center"
                      align="center"
                      show-overflow-tooltip
                      label="快递名称"
                    >
                    </el-table-column>
                    <el-table-column
                      prop="expressnumber"
                      header-align="center"
                      align="center"
                      show-overflow-tooltip
                      label="快递单号"
                    >
                    </el-table-column>
                    <el-table-column
                      prop="operator.name"
                      header-align="center"
                      align="center"
                      show-overflow-tooltip
                      label="操作人"
                    >
                    </el-table-column>
                    <el-table-column
                      prop="remarks"
                      header-align="center"
                      align="center"
                      show-overflow-tooltip
                      label="备注信息"
                    >
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" width="270" align="center">
                      <template slot-scope="scope">
                        <el-button
                          @click="viewGuestGiftRow(scope.row)"
                          type="text"
                          size="mini"
                          icon="el-icon-view"
                          >查看</el-button
                        >
                        <el-button
                          @click="editGuestGiftRow(scope.row)"
                          type="text"
                          size="mini"
                          icon="el-icon-edit"
                          >编辑</el-button
                        >
                        <el-button
                          @click="delGuestGiftRow(scope.row)"
                          type="text"
                          size="mini"
                          icon="el-icon-delete"
                          >删除</el-button
                        >
                        <el-button
                          v-if="scope.row.codeshow"
                          @click="sendMessageGuestGiftRow(scope.row)"
                          type="text"
                          size="mini"
                          icon="el-icon-s-promotion"
                          >短信通知</el-button
                        >
                        <el-button
                          v-if="scope.row.codehide"
                          type="text"
                          size="mini"
                          icon="el-icon-s-promotion"
                          >{{ scope.row.sendMessageNum }}s后通知</el-button
                        >
                      </template>
                    </el-table-column>
                  </el-table>
                </el-tab-pane>
              </el-tabs>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">关闭</el-button>
        <el-button
          size="small"
          type="primary"
          v-if="method != 'view'"
          @click="doSubmit()"
          v-noMoreClick
          >确定</el-button
        >
      </span>
    </el-dialog>
    <GuestGiftForm
      ref="guestGiftForm"
      @addRow="saveGuestGiftRow(arguments)"
    ></GuestGiftForm>
  </div>
</template>

<script>
import tableStyles from "../../../../../utils/mixins.js";
import GuestGiftForm from "./GuestGiftForm";
import GridSelect from "@/components/gridSelect";
import UserSelect from "@/components/userSelect";
export default {
  mixins: [tableStyles],
  data() {
    return {
      title: "",
      method: "",
      visible: false,
      loading: false,
      guestSentimentTab: "0",
      qualificationsArra:[],
      inputForm: {
        id: "",
        time: "",
        guestGiftList: [],
        customerName: {
          companyName:''
        },
        customerArea: "",
        personName: {
          userName:''
        },
        personPhone: "",
        personArea: "",
        personCard: "",
        personOffice: "",
        systemName: {
          id: "",
        },
        systemPhone: "",
        business: "",
        remarks: "",
        receivingSide:'',
        bankname:'',
        bankno:'',
        taxno:'',
        qualifications:'',
        endtime:'',
        billinginfo:'',
        billingarea:'',
        company:this.$store.state.user.companyId,
      },
      customerType:''//根据状态看能不能修改
    };
  },
  components: {
    GridSelect,
    UserSelect,
    GuestGiftForm,
  },
  methods: {
    getValue(value){
      this.inputForm.systemName.id=value[0];
      this.inputForm.systemPhone=value[2];
    },
    init(method, id) {
      this.method = method;
      if (method === "add") {
        this.title = `新建客情信息`;
      } else if (method === "edit") {
        this.title = "客情信息";
      } else if (method === "view") {
        this.title = "查看客情信息";
      }
      this.visible = true;
      this.loading = false;
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields();
        this.inputForm.id = id;
        this.guestSentimentTab = "0";
        this.inputForm.guestGiftList = [];
        this.qualificationsArra = [];
        if (method === "edit" || method === "view") {
          // 修改或者查看
          this.loading = true;
          this.$http({
            url: `/dlyrl/hr/guestsentiment/guestSentiment/queryById?id=${this.inputForm.id}`,
            method: "get",
          }).then(({ data }) => {
            console.log(data,'我要看customerType')
            this.customerType = data.guestSentiment.customerType
            this.inputForm = this.recover(this.inputForm, data.guestSentiment);
            this.inputForm.qualifications.split('|').forEach((item) => {
              if (item.trim().length > 0) {
                this.qualificationsArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
              }
            })
            this.loading = false;
          });
        }
      });
    },
    saveGuestGiftRow(child) {
      if (child[0] === "") {
        this.inputForm.guestGiftList.push(child[1]);
      } else {
        this.inputForm.guestGiftList.forEach((item, index) => {
          if (item === child[0]) {
            this.inputForm.guestGiftList.splice(index, 1, child[1]);
          }
        });
      }
    },
    addGuestGiftRow(child) {
      this.$refs.guestGiftForm.init("add");
    },
    viewGuestGiftRow(child) {
      this.$refs.guestGiftForm.init("view", child);
    },
    editGuestGiftRow(child) {
      this.$refs.guestGiftForm.init("edit", child);
    },
    delGuestGiftRow(child) {
      this.inputForm.guestGiftList.forEach((item, index) => {
        if (item === child && item.id === "") {
          this.inputForm.guestGiftList.splice(index, 1);
        } else if (item === child) {
          item.delFlag = "1";
          this.inputForm.guestGiftList.splice(index, 1, item);
        }
      });
    },
    sendMessageGuestGiftRow(child) {
      this.$http({
        url: "/dlyrl/hr/guestsentiment/guestSentiment/sendMessage",
        method: "get",
        params: {
          phone: this.inputForm.personPhone,
          name: this.inputForm.personName.userName,
          giftname: child.giftname,
          expressname: child.expressname,
          expressnumber: child.expressnumber,
        },
      }).then(({ data }) => {
        if (data && data.success) {
          child.codeshow = !child.codeshow;
          child.codehide = !child.codehide;
          let interval = setInterval(() => {
            --child.sendMessageNum;
          }, 1000);
          setTimeout(() => {
            clearInterval(interval);
            child.codeshow = !child.codeshow;
            child.codehide = !child.codehide;
          }, 60000);
        }
        child.sendMessageNum = "60";
      });
    },
    // 表单提交
    doSubmit() {
      this.$refs["inputForm"].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http({
            url: `/dlyrl/hr/guestsentiment/guestSentiment/save`,
            method: "post",
            data: this.inputForm,
          }).then(({ data }) => {
            this.loading = false;
            if (data && data.success) {
              this.visible = false;
              this.$message.success(data.msg);
              this.$emit("refreshDataList");
            }
          });
        }
      });
    },
  },
};
</script>